/** Header file for planet destruction
*	Created 17.9.2009 by Raymond; common.h massacre
*	*/
#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <vector>
#include "port.h"
#include "databank.h"

/** Structure for representing a mass point, used for mass center in each zone */
struct MassCenter {
    double pos[2];
    double mass;
};

// Single mass-center Zone for the background
class Zone {
	//BITMAP* partition; //connected bitmap to the background bitmap (if possible) -- Probably redundant
	MassCenter center;
	static int resolution;	// Size of each zone, shared between all centers
public:
	bool updated;
	bool full;
	int pos[2];

	void calculateCenter(BITMAP *bm);
	void setResolution(int res) {resolution = res;}
	int getResolution() { return resolution; }
	
	MassCenter *getCenter() { return(&center); }
};

class ZoneMap {
	int w,h;
	int resolution;
	Zone ***zmap;		// Making a map of zones, so they're easily accessible.
public:
	ZoneMap(int w, int h, int resolution);
	int getW() { return w; }
	int getH() { return h; }
	int getResolution() { return resolution; }
	
	Zone *getZone(int x,int y) { return zmap[y][x]; }
	void setZone(int x,int y, Zone *z) { zmap[y][x] = z; }
};

/** Handles the stationary background planet data */
class Background {
	BITMAP* background;
	std::vector<Zone *> zones;
	ZoneMap *zoneMap;
	DataBank *data;
	int magicPink;
	
	// Config info
	Config *cfg;
	
	void explodeParticle(int x,int y);
	void explodeFillInternal(int x,int y);
	void calculateEscape(int cx,int cy, double radius);
	
	// Floodfill commands
	struct LinePointer {
		int x,y;
		LinePointer(int x,int y) : x(x), y(y) {}
	};
	std::list<struct LinePointer> lines,edges;
	std::list<struct LinePointer> toAdd;
	void findLines(int x,int y);
	bool searchEdge(int x,int y);
	void traceLine(int x,int y);
public:
	double centPos[2];	// Temporary position for use by explosion
	double exVel[2];
	double exDir[2];
	double exRadius;
	double exPower;
	int recCount;
	
	Background(DataBank *d) { data = d; magicPink = makecol32(255,0,255); }
	DataBank *getData() { return data; }
	int getMagicPink(void) { return magicPink; }
	
	void init();
	void generateMap();
	void newGame();
	void clear();
	
	void updateZones();
	void updateAll();
	void explodeBackground(int pos[2],double vel[2], double radius, double power);
	
	bool testCollision(int x,int y) { return (_getpixel32(background,x,y)!=magicPink); }
	void clearParticle(int x,int y);
	void freezeParticle(int x,int y,int color);
	void explodeFill(int x,int y, double power);
	std::vector<Zone *> *getZones() { return &zones; }	// Faster than generating a vector of MassCenter
							// Kinda ruins the point of private variables.. but speed>>structure
	
	BITMAP *getBackground() { return background; }
};

#endif

